﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace tpd1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        List<StackPanel> lista;
        int row = 1, col = 1;
        Thickness margin = new Thickness(4, 4, 4, 4);
        List<TextBox> w4wsp;

        public MainWindow()
        {
            InitializeComponent();
            lista = new List<StackPanel>();
            StackPanel sP = new StackPanel();
            sP.Orientation = Orientation.Horizontal;
            sP.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            lista.Add(sP);
            TextBox tb = new TextBox();
            tb.Height = 30;
            tb.Width = 30;
            tb.Margin = margin;
            tb.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
            tb.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
            lista[0].Children.Add(tb);
            sPanel.Children.Add(lista[0]);

            w4wsp = new List<TextBox>();
            addw4wsp();
        }

        private void addw4wsp()
        {
            TextBox wspTb = new TextBox();
            wspTb.Height = 30;
            wspTb.Width = 30;
            wspTb.Margin = margin;
            wspTb.Text = "0,5";
            wspTb.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
            wspTb.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
            w4v.Children.Add(wspTb);
            w4wsp.Add(wspTb);
        }

        private void colBut_Click(object sender, RoutedEventArgs e)
        {
            foreach (StackPanel stkPnl in lista)
            {
                TextBox tb = new TextBox();
                tb.Height = 30;
                tb.Width = 30;
                tb.Margin = margin;
                tb.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
                tb.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
                stkPnl.Children.Add(tb);
            }
            col++;

            addw4wsp();
        }

        private void rowBut_Click(object sender, RoutedEventArgs e)
        {
            StackPanel sP = new StackPanel();
            sP.Orientation = Orientation.Horizontal;
            sP.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            for (int i = 0; i < col; i++)
            {
                TextBox tb = new TextBox();
                tb.Height = 30;
                tb.Width = 30;
                tb.Margin = margin;
                tb.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
                tb.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
                sP.Children.Add(tb);
            }
            lista.Add(sP);
            row++;
            sPanel.Children.Add(sP);
        }

        private void oblBut_Click(object sender, RoutedEventArgs e)
        {
            int[,] tab = new int[row, col];
            int i = 0, j = 0;
            Boolean noVal = false;
            foreach (StackPanel stP in lista)
            {
                j = 0;
                foreach (TextBox tb in stP.Children)
                {
                    tb.Background = Brushes.White;
                    try
                    {
                        tab[i, j] = Convert.ToInt32(tb.Text);
                    }
                    catch (System.FormatException)
                    {
                        tb.Background = Brushes.Crimson;
                        noVal = true;
                    }
                    j++;
                }
                i++;
            }

            if (!noVal)
            {
                kWalda(tab);
                kOpt(tab);
                kHurwicza(tab);
                kBayesa(tab);
                kSavage(tab);
            }
        }

        private void kWalda(int[,] tab)
        {
            int[] tab2 = new int[row];
            int max;
            for (int i = 0; i < row; i++)
            {
                max = tab[i, 0];
                for (int j = 0; j < col; j++)
                {
                    if (tab[i, j] > max)
                        max = tab[i, j];
                }
                tab2[i] = max;
            }

            int min = tab2[0];
            int wiersz = 0;
            for (int i = 0; i < row; i++)
            {
                if (tab2[i] < min)
                {
                    min = tab2[i];
                    wiersz = i;
                }
            }

            w1c1.Content = wiersz + 1;
            w1c2.Content = min;
        }

        private void kOpt(int[,] tab)
        {
            int[] tab2 = new int[row];
            int min;
            for (int i = 0; i < row; i++)
            {
                min = tab[i, 0];
                for (int j = 0; j < col; j++)
                {
                    if (tab[i, j] < min)
                        min = tab[i, j];
                }
                tab2[i] = min;
            }

            int min2 = tab2[0];
            int wiersz = 0;
            for (int i = 0; i < row; i++)
            {
                if (tab2[i] < min2)
                {
                    min2 = tab2[i];
                    wiersz = i;
                }
            }

            w2c1.Content = wiersz + 1;
            w2c2.Content = min2;
        }

        private void kHurwicza(int[,] tab)
        {
            float[] tab2 = new float[row];
            int min;
            int max;
            float wsp;

            try
            {
                wsp = Convert.ToSingle(w3v.Text);
                w3v.Background = Brushes.White;
            }
            catch (System.FormatException)
            {
                w3v.Background = Brushes.Crimson;
                wsp = 0.5f;
            }

            for (int i = 0; i < row; i++)
            {
                min = tab[i, 0];
                max = tab[i, 0];
                for (int j = 0; j < col; j++)
                {
                    if (tab[i, j] < min)
                        min = tab[i, j];

                    if (tab[i, j] > max)
                        max = tab[i, j];
                }
                tab2[i] = min * wsp + max * (1-wsp);
            }

            float min2 = tab2[0];
            int wiersz = 0;
            for (int i = 0; i < row; i++)
            {
                if (tab2[i] < min2)
                {
                    min2 = tab2[i];
                    wiersz = i;
                }
            }

            w3c1.Content = wiersz + 1;
            w3c2.Content = min2;
        }

        private void kBayesa(int[,] tab)
        {
            float[] wsp = new float[col];

            int c = 0;
            foreach (TextBox tb in w4v.Children)
            {
                try
                {
                    tb.Background = Brushes.White;
                    wsp[c] = Convert.ToSingle(tb.Text);
                }
                catch (System.FormatException)
                {
                    tb.Background = Brushes.Crimson;
                    wsp[c] = 0.5f;
                }
                c++;
            }

            float[] tab2 = new float[row];
            float suma;
            for (int i = 0; i < row; i++)
            {
                suma = 0;
                for (int j = 0; j < col; j++)
                {
                    suma += tab[i, j] * wsp[j];
                }
                tab2[i] = suma;
            }

            float min = tab2[0];
            int wiersz=0;
            for (int i = 0; i < row; i++)
            {
                if (tab2[i]<min)
                {
                    min = tab2[i];
                    wiersz = i;
                }
            }

            w4c1.Content = wiersz + 1;
            w4c2.Content = min;
        }

        private void kSavage(int[,] tab)
        {
            int[] tab2 = new int[row];
            int min;
            int max;
            for (int i = 0; i < row; i++)
            {
                min = tab[i, 0];
                max = tab[i, 0];
                for (int j = 0; j < col; j++)
                {
                    if (tab[i, j] < min)
                        min = tab[i, j];

                    if (tab[i, j] > max)
                        max = tab[i, j];
                }
                tab2[i] = max-min;
            }

            int min2 = tab2[0];
            int wiersz = 0;
            for (int i = 0; i < row; i++)
            {
                if (tab2[i] < min2)
                {
                    min2 = tab2[i];
                    wiersz = i;
                }
            }

            w5c1.Content = wiersz + 1;
            w5c2.Content = min2;

        }
    }
}
